﻿Public Class Wfrm_Mapeo
    Inherits Wfrm_Master
    Dim objetos As Integer = 18

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Try
                'Dim cadenaCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Session("archivo") & ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"

                Dim cadenaCon As String
                If System.IO.Path.GetExtension(Session("archivo")).ToUpper() = ".XLS" Then
                    cadenaCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Session("archivo") & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=0"";"
                Else
                    cadenaCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Session("archivo") & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=0"";"
                End If

                Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection(cadenaCon)
                con.Open()

                Dim adaptador1 As New OleDb.OleDbDataAdapter("SELECT * FROM [PERSONAL$]", con)
                Dim datos1 As New DataSet
                adaptador1.Fill(datos1, "XLData")
                con.Close()


                Dim i As Integer


                If Not datos1.Tables Is Nothing Then
                    Dim dt As New DataTable
                    Dim dr As DataRow
                    dt.Columns.Add("D")
                    dt.Columns.Add("C")

                    dr = dt.NewRow
                    dr(0) = "Sin Mapear"
                    dr(1) = "-1"

                    dt.Rows.Add(dr)
                    For i = 0 To datos1.Tables(0).Columns.Count - 1
                        If datos1.Tables(0).Columns(i).ColumnName <> "ROW" Then
                            dr = dt.NewRow
                            dr(0) = datos1.Tables(0).Columns(i).ColumnName
                            dr(1) = datos1.Tables(0).Columns(i).ColumnName
                            dt.Rows.Add(dr)
                        End If
                    Next

                    For j = 0 To objetos
                        Try
                            Dim cmb As New Anthem.DropDownList
                            cmb = DirectCast(Form.FindControl("cmb_excel" & j.ToString), Anthem.DropDownList)
                            cmb.DataValueField = "C"
                            cmb.DataTextField = "D"
                            cmb.DataSource = dt
                            cmb.DataBind()
                        Catch ex As Exception

                        End Try

                    Next
                End If

                '    For i = 0 To datos1.Tables(0).Columns.Count - 1

                '        Dim j As Integer

                '        If i = 0 Then
                '            Dim item As New ListItem
                '            item.Text = "Sin Mapear"
                '            item.Value = "-1"

                '            cmb_excel0.Items.Add(item)

                '            'For j = 0 To 17
                '            '    Dim cmb As New Anthem.DropDownList
                '            '    cmb = DirectCast(Form.FindControl("cmb_excel" & j.ToString), Anthem.DropDownList)
                '            '    cmb.Items.Add(item)
                '            'Next
                '        End If





                '        For j = 0 To 17

                '        Next


                '        'Dim item2 As New ListItem
                '        'item2.Text = datos1.Tables(0).Columns(i).ColumnName
                '        'item2.Value = datos1.Tables(0).Columns(i).ColumnName

                '        'cmb_excel0.Items.Add(item2)

                '        'cmb_excel0.Items.Add(item2)


                '        'For j = 0 To 17
                '        '    Dim cmb As New Anthem.DropDownList
                '        '    cmb = DirectCast(Form.FindControl("cmb_excel" & j.ToString), Anthem.DropDownList)
                '        '    cmb.Items.Add(item2)
                '        '    DirectCast(Form.FindControl("cmb_excel" & j.ToString), Anthem.DropDownList).Items.Add(item)
                '        'Next
                '    Next
                'End If


                Try
                    Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
                    Dim rp As New DotNetResponse.SQLPersistence
                    rp.StoredProcedure = "SP_GOP_GET_UP_CAMPO"
                    rp.AddParametro(Session("sociedad"))
                    exec.ExecuteDataTable(rp)
                    If rp.TieneDatos Then
                        For i = 0 To rp.DtTable.Rows.Count - 1
                            For j = 0 To objetos
                                Try
                                    Dim cmb As New Anthem.DropDownList
                                    cmb = DirectCast(Form.FindControl("cmb_excel" & j.ToString), Anthem.DropDownList)

                                    If DirectCast(Form.FindControl("cmb_excel" & j.ToString), Anthem.DropDownList).SkinID = rp.DtTable.Rows(i).Item("TABLA").ToString Then
                                        DirectCast(Form.FindControl("cmb_excel" & j.ToString), Anthem.DropDownList).SelectedValue = rp.DtTable.Rows(i).Item("CAMPO").ToString

                                        Exit For
                                    End If

                                    If cmb.SkinID = rp.DtTable.Rows(i).Item("TABLA").ToString Then
                                        Dim k As Integer
                                        For k = 0 To cmb.Items.Count - 1
                                            If cmb.Items(k).Value = rp.DtTable.Rows(i).Item("CAMPO").ToString Then
                                                cmb.Items(k).Selected = True
                                            End If
                                        Next

                                        Exit For
                                    End If

                                Catch ex As Exception
                                End Try
                            Next
                        Next
                    End If

                    exec.Commit()

                Catch ex As Exception

                End Try
            Catch ex As Exception

            End Try

        End If
    End Sub

    Protected Sub Btn_Agregar_Pregunta_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Agregar_Pregunta.Click
        'GRABAR LA CONFIGURACION
        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_GOP_SAVE_UP_CAMPO"
        For j = 0 To objetos
            Try
                rp.ClearParametro()
                rp.Errores = False
                rp.AddParametro(DirectCast(Form.FindControl("cmb_excel" & j.ToString), Anthem.DropDownList).SkinID)
                rp.AddParametro(DirectCast(Form.FindControl("cmb_excel" & j.ToString), Anthem.DropDownList).SelectedValue)
                rp.AddParametro(j)
                rp.AddParametro("")
                rp.AddParametro(Session("sociedad"))
                exec.ExecuteNonQuery(rp)
            Catch ex As Exception

            End Try

        Next
        exec.Commit()

        If rp.Errores Then
            MsgError(rp.MensajeError)
        Else
            Response.Redirect("WFRM_CONFIRMAR_MAPEO.ASPX")
        End If
    End Sub
End Class